#include "index.h"
#include "ui_index.h"

Index::Index(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::Index)
{
    ui->setupUi(this);
    QPalette pe;
    pe.setColor(QPalette::WindowText,Qt::red);
    ui->label_5->setText("Stopped");
    ui->label_5->setPalette(pe);
    get_nam = new QNetworkAccessManager(this);
    post_nam = new QNetworkAccessManager(this);
    leave_nam = new QNetworkAccessManager(this);
    QJsonObject obj = this->getIpMAC();
    get_url = new QString("http://202.114.240.111/eportal/pcLogin_0269f68528ce49769904ed56d4c63655.html?fromHtml=true&version=52472d65506f7274616c20454e54455250524953455f312e3433287032295f4275696c643230313430333036&userAgentForLogin=0&wlanuserip="
                          +obj.value("wlanuserip").toString()
                          +"&wlanacname=Ruijie_Ac_800a2e&ssid=WUST_Wireless&nasip=10.100.0.154&mac="
                          +obj.value("mac").toString()
                          +"&t=wireless-v2-plain&url=http://www.baidu.com&h=4000&c=&n=Titon%20Toolkit&pageUuid=0269f68528ce49769904ed56d4c63655");
    post_url = new QString("http://202.114.240.111/eportal/userV2.do?method=login&param=true&fromHtml=true&version=52472d65506f7274616c20454e54455250524953455f312e3433287032295f4275696c643230313430333036&userAgentForLogin=0&wlanuserip="
                           +obj.value("wlanuserip").toString()
                           +"&wlanacname=Ruijie_Ac_800a2e&ssid=WUST_Wireless&nasip=10.100.0.154&mac="
                           +obj.value("mac").toString()
                           +"&t=wireless-v2-plain&url=https://www.baidu.com&pageUuid=0269f68528ce49769904ed56d4c63655");
    leave_url = new QString();
    connect(get_nam, SIGNAL(finished(QNetworkReply*)), this, SLOT(get_finished(QNetworkReply*)));
    connect(post_nam, SIGNAL(finished(QNetworkReply*)), this, SLOT(post_finished(QNetworkReply*)));
    connect(leave_nam, SIGNAL(finished(QNetworkReply*)), this, SLOT(leave_finished(QNetworkReply*)));
}

Index::~Index()
{
    delete ui;
}


void Index::get_finished(QNetworkReply *reply){
    if(reply->error()==QNetworkReply::NoError){
        QVariant variantCookies = reply->header(QNetworkRequest::SetCookieHeader);
        QList<QNetworkCookie> cookies = qvariant_cast<QList<QNetworkCookie> >(variantCookies);
 //       qDebug() << "Cookies reply: " << cookies;
        QNetworkCookie cookie; //Create a cookie

        QNetworkCookieJar *jar = new QNetworkCookieJar();
        //view->page()->networkAccessManager()->setCookieJar(jar);
   //     qDebug() << "Hello World:\n";
  //      qDebug() << jar->setCookiesFromUrl(cookies, reply->request().url());
        //qDebug() << "Saved cookies: " << jar->getAllCookies();
        QString username = ui->lineEdit->text();
        QString password = ui->lineEdit_2->text();
        QString paras("usernameHidden="
                      +username
                      +"&username="
                      +username
                      +"&authorMode=&pwd="
                      +password
                      +"&phone=&authorizationCode=&regist_validcode=&phonenum=&regist_validcode_sm=&authorCode=");
        QByteArray post_data;
        post_data.append(paras);
        QNetworkRequest request = QNetworkRequest(QUrl(*post_url));
        request.setRawHeader("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8");
        request.setRawHeader("Accept-Encoding","gzip, deflate, sdch");
        request.setRawHeader("Accept-Language","zh-CN,zh;q=0.8,en-US;q=0.5,en;q=0.3");
        request.setRawHeader("Upgrade-Insecure-Requests","1");
        request.setRawHeader("User-Agent","Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/51.0.2704.84 Safari/537.36");
        request.setRawHeader("Connection","keep-alive");
        request.setRawHeader("Referer",get_url->toUtf8());
        post_nam->setCookieJar(jar);
  //      qDebug() << request.rawHeaderList();
   //     qDebug() << post_data;
        post_nam->post(request,post_data);
    }

  //  qDebug() << "Network reply: " << reply->errorString() << reply->error() << reply->request().url();


}
void Index::post_finished(QNetworkReply *reply){
    if(reply->error()==QNetworkReply::NoError){
        QString content = reply->readAll();
        QRegExp macRE(QString("mac=[0-9a-z]{32}"));
        QRegExp wlanuseripRE(QString("wlanuserip=[0-9a-z]{32}"));
        QRegExp nasipRE(QString("nasip=[0-9a-z]{32}"));
        int macIndex = content.indexOf(macRE);
        int wlanuseripIndex = content.indexOf(wlanuseripRE);
        int nasipIndex = content.indexOf(nasipRE);
        if ( macIndex >= 0 && wlanuseripIndex>=0 && nasipIndex >=0){
            leave_url = new QString("http://202.114.240.111/eportal/userV2.do?method=logout&"
                                +macRE.cap(0)+'&'
                                +wlanuseripRE.cap(0)+'&'
                                +nasipRE.cap(0)
                        );
        }
        QPalette pe;
        pe.setColor(QPalette::WindowText,Qt::green);
        ui->label_5->setText("Running");
        ui->label_5->setPalette(pe);
    }
    if(!ui->pushButton->isEnabled())
        ui->pushButton->setEnabled(true);
    if(!ui->pushButton_2->isEnabled())
        ui->pushButton_2->setEnabled(true);
}

void Index::on_pushButton_clicked()
{
    if(ui->pushButton->isEnabled())
        ui->pushButton->setEnabled(false);
    if(ui->pushButton_2->isEnabled())
        ui->pushButton_2->setEnabled(false);

    QNetworkRequest request = QNetworkRequest(QUrl(*get_url));
    request.setRawHeader("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8");
    request.setRawHeader("Accept-Encoding","gzip, deflate, sdch");
    request.setRawHeader("Accept-Language","zh-CN,zh;q=0.8,en-US;q=0.5,en;q=0.3");
    request.setRawHeader("Upgrade-Insecure-Requests","1");
    request.setRawHeader("User-Agent","Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/51.0.2704.84 Safari/537.36");
    request.setRawHeader("Connection","keep-alive");
    request.setRawHeader("content-type", "application/x-www-form-urlencoded");
    get_nam->get(request);
}

void Index::on_pushButton_2_clicked()
{
    if(ui->pushButton->isEnabled())
        ui->pushButton->setEnabled(false);
    if(ui->pushButton_2->isEnabled())
        ui->pushButton_2->setEnabled(false);
    if(leave_url!=QString()){
        QNetworkRequest request = QNetworkRequest(QUrl(*leave_url));
        request.setRawHeader("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8");
        request.setRawHeader("Accept-Encoding","gzip, deflate, sdch");
        request.setRawHeader("Accept-Language","zh-CN,zh;q=0.8,en-US;q=0.5,en;q=0.3");
        request.setRawHeader("Upgrade-Insecure-Requests","1");
        request.setRawHeader("User-Agent","Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/51.0.2704.84 Safari/537.36");
        request.setRawHeader("Connection","keep-alive");
        request.setRawHeader("content-type", "application/x-www-form-urlencoded");
        leave_nam->get(request);
    }
}
QJsonObject Index::getIpMAC(){
    QJsonObject json = QJsonObject();
    foreach(QNetworkInterface netInterface, QNetworkInterface::allInterfaces())
    {
       if (!(netInterface.flags() & QNetworkInterface::IsLoopBack )
               &&(netInterface.flags() & QNetworkInterface::IsRunning )
               &&(netInterface.flags() & QNetworkInterface::IsUp )
               &&(netInterface.flags() & QNetworkInterface::CanBroadcast )
               &&(netInterface.flags() & QNetworkInterface::CanMulticast )
               ){
           foreach (QNetworkAddressEntry netEntry,netInterface.addressEntries()){
                if(netEntry.ip().toIPv4Address() != 0){
                    json.insert("mac", QString(netInterface.hardwareAddress().data()).toLower().replace(QString(":"),QString("")));
                    json.insert("wlanuserip", netEntry.ip().toString());
                    return json;
                }
            }
        }
    }
    return json;
}
void Index::leave_finished(QNetworkReply *reply){
    if(reply->error() == QNetworkReply::NoError){

    }
    QPalette pe;
    pe.setColor(QPalette::WindowText,Qt::red);
    ui->label_5->setText("Stopped");
    ui->label_5->setPalette(pe);
    if(ui->pushButton->isEnabled())
        ui->pushButton->setEnabled(false);
    if(ui->pushButton_2->isEnabled())
        ui->pushButton_2->setEnabled(false);

    QNetworkRequest request = QNetworkRequest(QUrl(*leave_url));
    request.setRawHeader("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8");
    request.setRawHeader("Accept-Encoding","gzip, deflate, sdch");
    request.setRawHeader("Accept-Language","zh-CN,zh;q=0.8,en-US;q=0.5,en;q=0.3");
    request.setRawHeader("Upgrade-Insecure-Requests","1");
    request.setRawHeader("User-Agent","Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/51.0.2704.84 Safari/537.36");
    request.setRawHeader("Connection","keep-alive");
    request.setRawHeader("content-type", "application/x-www-form-urlencoded");
    get_nam->get(request);
}
